﻿using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using Furion;
using Furion.FriendlyException;
using Org.BouncyCastle.Bcpg.OpenPgp;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Api.Ali
{
    /// <summary>
    /// 支付宝支付接口
    /// </summary>
    public class Alipay : ITransient
    {
        private readonly ISqlSugarClient db;

        private string charset = "utf-8";

        private string domain = "https://openapi.alipay.com/gateway.do";

        private string AppPrivate = "";

        private string AppId = "";

        private CertParams CertParams;

        IAopClient client;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="db"></param>
        public Alipay(ISqlSugarClient db)
        {
            this.db = db;

            this.AppId = App.Configuration.GetSection("AliPay:AppId").Value ?? throw Oops.Oh("缺失AppId");

            this.AppPrivate = App.Configuration.GetSection("AliPay:AppPrivate").Value ?? throw Oops.Oh("缺失应用私钥");

            this.CertParams = new CertParams
            {
                AppCertPath = App.Configuration.GetSection("AliPay:AppCert").Value,

                AlipayPublicCertPath = App.Configuration.GetSection("AliPay:AliCert").Value,

                RootCertPath = App.Configuration.GetSection("AliPay:RootCert").Value
            };

            client = new DefaultAopClient(domain, AppId, AppPrivate, "json", "1.0", "RSA2", charset, false, CertParams);
        }



        /// <summary>
        /// 通用提现
        /// </summary>
        /// <param name="price"></param>
        /// <param name="orderNo"></param>
        /// <param name="account"></param>
        /// <param name="name"></param>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public (bool, string) Tran(decimal price, string orderNo, string account, string name)
        {

            // 构造请求参数以调用接口
            AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();

            // 设置请求参数
            AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();

            // 设置商家侧唯一订单号
            model.OutBizNo = orderNo;

            // 设置订单总金额
            model.TransAmount = price.ToString("F2");

            // 设置描述特定的业务场景
            model.BizScene = "DIRECT_TRANSFER";

            // 设置业务产品码
            model.ProductCode = "TRANS_ACCOUNT_NO_PWD";

            // 设置转账业务的标题
            model.OrderTitle = "账户提现";

            model.PayeeInfo = new Participant
            {

                IdentityType = "ALIPAY_LOGON_ID",

                Identity = account,

                Name = name
            };

            // 设置业务备注
            model.Remark = "账户提现";

            request.SetBizModel(model);

            AlipayFundTransUniTransferResponse response = client.CertificateExecute(request);

            if (response.Code == "10000" && response.Status == "SUCCESS") return (true, response.OrderId);

            return (false, response.SubMsg);

        }
    }
}
